package com.mh.config;

import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import jakarta.annotation.PreDestroy;
import lombok.Data;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * Date:2025/5/8
 * author:zmh
 * description: OSS配置
 **/

@Configuration
@Data
public class OssConfig {

    // 到Bucket的【概览】>【访问端口】中查看
    private String endpoint = "oss-cn-guangzhou.aliyuncs.com";

    // 上传到的Bucket名称
    private String bucket = "uni-manage-1746695741875-2576";

    // 指定上传到 OSS 的文件前缀，就是上传到Bucket的哪个目录下
    private String dir = "0509/";

    // 指定签名过期时间，单位为秒
    private long expireTime = 3600;

    // 构造 host，可以给前端用来拼接图片回显地址
    private String host = "http://" + bucket + "." + endpoint;

    // KeyId 和 KeySecret 请到application.yml中进行配置
    @Value("${oss.accessKeyId}")
    private String accessKeyId;

    @Value("${oss.secretAccessKey}")
    private String accessKeySecret;

    // OSS客户端，根据上方参数在下面进行构造
    private OSS ossClient;

    @Bean
    public OSS getOssClient() {
        ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
        return ossClient;
    }

    @PreDestroy
    public void onDestroy() {
        ossClient.shutdown();
    }
}
